package model;

import java.util.logging.Level;
import java.util.logging.Logger;
import org.zkoss.zk.ui.SuspendNotAllowedException;
import org.zkoss.zk.ui.util.*;
import org.zkoss.zul.*;
import java.util.List;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import org.zkoss.web.portlet.RenderHttpServletRequest;
import org.zkoss.web.servlet.xel.RequestContext;
import org.zkoss.web.servlet.xel.RequestContexts;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.Session;
import org.zkoss.zk.ui.Sessions;
import org.zkoss.zk.ui.ext.AfterCompose;
import flexjson.*;
/**
 * @author JS: Juan Sanjuan
 * This Class Controls the behibor and events of 
 * controls  at index.zul
 */
public class IndiceControlador extends GenericAutowireComposer  {
    private static final long serialVersionUID = -9145887024839938515L;
    private Menuitem iniciar;
    private Image imgMapa;
    
    //PaiPais objPais;
    
    /*private EventDAO eventDao = new EventDAO();

    // Note: Something is omitted at here. You can view detail about this class on source code.

    public List<TodoEvent> getAllEvents() {
        return eventDao.findAll();
    }*/
    
    public void onClickNuevo()
    {
        try
        {
            String[] objArgs={""};
            Ice.Communicator objComunicator = Ice.Util.initialize(objArgs);
            SistogeoICE.CommunicationSistogeoPrx objEjecutar = 
                    SistogeoICE.CommunicationSistogeoPrxHelper.checkedCast(
                            objComunicator.stringToProxy("CommunicationSistogeo:tcp -p 10002"));
            String objRetorno = objEjecutar.requestFunction("yeap",  "wowwwww");
            iniciar.setLabel(objRetorno);
            objComunicator.destroy();
        }
        catch(Ice.LocalException ex)
        {
            ex.printStackTrace();
            System.exit(1);
        }
    }

    public void onIniciar() {
        try
        {
            String[] objArgs={""};
            Ice.Communicator objComunicator = Ice.Util.initialize(objArgs);
            SistogeoICE.CommunicationSistogeoPrx objEjecutar = 
                    SistogeoICE.CommunicationSistogeoPrxHelper.checkedCast(
                            objComunicator.stringToProxy("CommunicationSistogeo:tcp -p 10002"));
            DatosMapa objDatosMapa=new DatosMapa();
            objDatosMapa.x= 1155019.0F;
            objDatosMapa.y= 994641.188F;
            objDatosMapa.z= 20F;
            objDatosMapa.Alto = 600;
            objDatosMapa.Largo = 800;
            JSONSerializer objJson= new JSONSerializer();
            String strDatosFuncion =objJson.serialize(objDatosMapa);
            String strRetorno = objEjecutar.requestFunction("yeap",  strDatosFuncion);
            iniciar.setLabel(strRetorno);
            imgMapa.setSrc("mapas?ID=" + strRetorno);
            objComunicator.destroy();
        }
        catch(Ice.LocalException ex)
        {
            Logger.getLogger(IndiceControlador.class.getName()).log(Level.SEVERE, null, ex);
        }
        //EntityManagerFactory emf = javax.persistence.Persistence.createEntityManagerFactory("HojasVidaPU");
	//EntityManager em = emf.createEntityManager();
        //List<PaiPais> paises= em.createQuery("SELECT p FROM pai_pais p").getResultList();
        //objPais= em.find(PaiPais.class ,1);
        //iniciar.setLabel("Que chimba parceee" + objPais.getPaiNombre());
        //iniciar.setSrc("img/exit.png");

        /*EntityManagerFactory emf = javax.persistence.Persistence.createEntityManagerFactory("HojasVidaPU");
        EntityManager em = emf.createEntityManager();
        GenericDaoFactory factory = GenericDaoFactory.create(em);
        PaisDao dao = factory.getDao(PaisDao.class);
        objPais = dao.findByPaiId(8);
        iniciar.setLabel("Que chimba parceee" + objPais.getPaiNombre());
        //iniciar.*/

    }

    public void onCrear()
    {
        try
        {
            /*if(super.execution.isUserInRole("_applied"))
            {

            } else
            {
                final Window win = (Window) Executions.createComponents(
            			"login.zul", null, null);
                win.setParent(self.getFellow("principal"));
                win.doModal();
                //Session objSession = Sessions.getCurrent();
                //objSession.get
            }*/
        } catch (Exception ex)
        {
            Logger.getLogger(IndiceControlador.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

}
